﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Threading;
using Microsoft.Ccr.Core;

namespace XcoAppSpaces.Dialogs
{
    public class StatemachineDialogMessageHandler<TMessage, TStates> : DialogMessageHandler<TMessage>
    {
        private StatemachineDialog<TStates> dialog;
        private bool runInSyncContext;
        private Func<TMessage, bool> customCondition;
        private Func<bool> checkFromStateCondition;
        private Action setNewState;
        
        
        internal StatemachineDialogMessageHandler(StatemachineDialog<TStates> dialog, SynchronizationContext syncContext)
        {
            this.dialog = dialog;
            base.syncContext = syncContext;

            this.customCondition = m => true;
            this.checkFromStateCondition = () => true;
            this.setNewState = delegate { };
            base.condition = m => true;
        }

        public StatemachineDialogMessageHandler<TMessage, TStates> FromState(TStates currentState)
        {
            this.checkFromStateCondition = () => this.dialog.State.Equals(currentState);
            return this;
        }

        public StatemachineDialogMessageHandler<TMessage, TStates> ToState(TStates newState)
        {
            this.setNewState = () => this.dialog.State = newState;
            return this;
        }

        public StatemachineDialogMessageHandler<TMessage, TStates> When(Func<TMessage, bool> condition)
        {
            this.customCondition = condition;
            return this;
        }

        public StatemachineDialogMessageHandler<TMessage, TStates> InSyncContext()
        {
            this.runInSyncContext = true;
            return this;
        }

        public void By(Handler<TMessage> handler)
        {
            if (!this.runInSyncContext) base.syncContext = null;

            base.condition = m => this.checkFromStateCondition() && this.customCondition(m);

            base.handler = message => { 
                                        handler(message);
                                        this.setNewState();
                                      };
        }
    }
}
